package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.spc_cl_report.SpcClReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.ISpcClReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.ComputeUtil.formatDouble2;

@Slf4j
@Service
public class SpcClReportServiceImpl implements ISpcClReportService {


    public List<SpcClReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("A.*");
        sqlVo.appendSql("FROM icim_jn.spc_cl_suggest A");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getCollect_type())) sqlVo.appendSql("AND A.COL_TYP_FK in" + jointInSql(conditions.getCollect_type()));
        if (StringUtils.isNotEmpty(conditions.getReport_type())) sqlVo.appendSql("AND A.RPT_TYP_FK in" + jointInSql(conditions.getReport_type()));
        if (StringUtils.isNotEmpty(conditions.getControl_chart_grp())) {
            String grpNo_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String colType_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[1]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String grpName_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND A.grp_no_fk in" + grpNo_list_str);
            sqlVo.appendSql("AND A.col_typ_fk in" + colType_list_str);
            sqlVo.appendSql("AND A.grp_name_fk in" + grpName_list_str);
        }
        if (StringUtils.isNotEmpty(conditions.getControl_chart())) {
            String chartNo_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartTyp_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[1]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartName_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND A.chart_no_fk in" + chartNo_list_str);
            sqlVo.appendSql("AND A.chart_typ_fk in" + chartTyp_list_str);
            sqlVo.appendSql("AND A.chart_name_fk in" + chartName_list_str);
        }
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.CALC_E_PROC_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.CALC_B_PROC_TIMESTAMP <= ?", conditions.getEnd_time());
        sqlVo.appendSql("ORDER BY A.CALC_E_PROC_TIMESTAMP");

        List<SpcClReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, SpcClReportGridOut.class);

        /*** 四舍五入保留2位小数。 去除掉input_timestamp的 .0 **/
        resultDataList.forEach(data -> {
            if (StringUtils.isNotEmpty(data.getCalc_ucl())) data.setCalc_ucl(formatDouble2(data.getCalc_ucl()).toString());
            if (StringUtils.isNotEmpty(data.getCalc_target())) data.setCalc_target(formatDouble2(data.getCalc_target()).toString());
            if (StringUtils.isNotEmpty(data.getCalc_lcl())) data.setCalc_lcl(formatDouble2(data.getCalc_lcl()).toString());

            if (StringUtils.isNotEmpty(data.getCalc_b_proc_timestamp())) data.setCalc_b_proc_timestamp(data.getCalc_b_proc_timestamp().substring(0, 19));
            if (StringUtils.isNotEmpty(data.getCalc_e_proc_timestamp())) data.setCalc_e_proc_timestamp(data.getCalc_e_proc_timestamp().substring(0, 19));
        });

        return resultDataList;
    }



}
